//
//  selectBarrier.m
//  footBoll
//
//  Created by zhoubaitong on 2017/10/10.
//  Copyright © 2017年 cckv. All rights reserved.
//

#import "selectBarrier.h"
#import "CollectionViewCell.h"

#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height

@interface selectBarrier()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (weak, nonatomic) IBOutlet UIView *middleView;

@property (strong, nonatomic) UICollectionView * collectionView;

@property (strong, nonatomic) NSArray *dataArray;

@end

@implementation selectBarrier
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUI];
    }
    return self;
}

-(void)awakeFromNib
{
    [super awakeFromNib];
    
    self.layer.cornerRadius = 10;
    self.layer.masksToBounds = YES;
    
    [self setUI];
}

- (void)setUI
{
    [self.middleView addSubview:self.collectionView];
    self.collectionView.frame = self.middleView.bounds;
    
    self.dataArray = [dataModel findAll];
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    self.collectionView.frame = self.middleView.bounds;
    self.collectionView.backgroundColor = [UIColor whiteColor];
}

#pragma mark - collectionView的代理方发
//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 12;
}

//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([CollectionViewCell class]) forIndexPath:indexPath];
    
//    NSArray *arr = [dataModel findAll];
//    NSArray *arr = [[NSUserDefaults standardUserDefaults]objectForKey:@"dataArray"];
    
    cell.model = self.dataArray[indexPath.row];
    
//    cell.topic = [NSString stringWithFormat:@"%ld",indexPath.row+1];
    
    return cell;
    
}

#pragma mark --UICollectionViewDelegateFlowLayout

//定义每个Item 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(60, 60);
}

//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5, 5, 5, 5);
}

#pragma mark --UICollectionViewDelegate

//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    dataModel *model = self.dataArray[indexPath.row];
    
    if (model.pass && model.levelIndex<3) {
        if ([self.delegate respondsToSelector:@selector(chooseChannelWithIndex:)]) {
            [self.delegate chooseChannelWithIndex:indexPath.row];
        }
    }else{
        NSString *str = nil;
        if (model.levelIndex<3) {
            str = NSLocalizedString(@"关卡暂未解锁", nil);
        }else{
            str = NSLocalizedString(@"敬请期待", nil);
        }
        [SVProgressHUD showErrorWithStatus:str];
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
        [SVProgressHUD dismissWithDelay:1];

    }

    
}

//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

-(UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
        flowLayout.minimumLineSpacing = 10;
        flowLayout.minimumInteritemSpacing = 10;
        [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
        UICollectionView *cv = [[UICollectionView alloc] initWithFrame:self.middleView.bounds collectionViewLayout:flowLayout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView = cv;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([CollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([CollectionViewCell class])];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        
    }
    return _collectionView;
}
- (IBAction)exit:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(clickBackButton)]) {
        [self.delegate clickBackButton];
    }
}

-(NSArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSArray array];
    }
    return _dataArray;
}

@end
